package com.neusoft.elmboot.controller;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.elmboot.service.PointService;
import com.neusoft.elmboot.service.VirtualWalletService;
import com.neusoft.elmboot.util.APIResponse;

@RestController
@RequestMapping("/VirtualWallet")
public class VirtualWalletController {
	
	@Autowired
	private VirtualWalletService virtualWalletService;
	
	@Autowired
	private PointService pointservice;
	
	@GetMapping("/balance")
	public APIResponse getBalanceByUserId(String userId) {
		return APIResponse.success(virtualWalletService.getBalance(userId));
	}
	
	@PostMapping("/RechargeOrWithdraw")
	public APIResponse RechargeOrWithdraw(String userId, String password,BigDecimal amount) throws Exception {
		return APIResponse.success(virtualWalletService.RechargeOrWithdraw(userId, password, amount));
	}
	
	@PostMapping("/Pay")
	@Transactional
	public APIResponse PayByUserIdPasswordOrderId(String userId, String password,int orderId) throws Exception {
		virtualWalletService.payByOrderId(userId,password, orderId);
		pointservice.consumePoint(orderId, userId);
		return APIResponse.success(pointservice.createPoint(orderId, userId));
	}
	
	@GetMapping("/transaction")
	public APIResponse getTranscationsByUserId(String userId) throws Exception {
		return APIResponse.success(virtualWalletService.getTransactionRecordByUserId(userId));
	}
	
	
}
